<?php

namespace App\Trip\Orders\ApplyRefund;

use App\Models\Train\TrainOrderPassengers\TrainOrderPassenger;
use App\Models\Train\TrainOrders\TrainOrder;
use App\Trip\Orders\Traits\OfflineOrderChangeOrRefundTimeValidTrait;
use App\Trip\Orders\Traits\ThrowErrorMessageTrait;
use App\Trip\Orders\TripOrderState;
use Carbon\Carbon;
use Exception;

class TrainOrderApplyRefund implements OrderRefund
{
    use ThrowErrorMessageTrait, OfflineOrderChangeOrRefundTimeValidTrait;

    private TrainOrderPassenger $passenger;
    private TrainOrder          $order_info;
    private bool                $ignore_worktime_error = false;
    private bool                $has_other_error       = false;

    public function __construct(TrainOrder $order_info, TrainOrderPassenger $passenger)
    {
        $this->passenger  = $passenger;
        $this->order_info = $order_info;
    }

    public function canApplyRefundToShow(): bool
    {
        $res     = $this->setIgnoreWorkTimeError()->canApplyRefund();
        $log_res = $res ? 'true' : 'false';
//		Log::info("canApplyRefundToShow:res:{$log_res}");
        return $res;
    }

    /**
     * 是否能申请退票退款
     *
     * @return bool
     */
    public function canApplyRefund(): bool
    {
        try {
            $this->validOrderState();
            $this->validTime();
            $this->validOrder();
            if (!$this->has_ticket()) {
                $this->throwOtherErrorMsg('乘客未出票,不支持此退款');
            }
            return true;
        } catch (Exception $e) {
//			Log::error("canApplyRefund:{$this->order_info->getOrderSn()},hasOtherError:{$this->hasOtherError()},ignore_worktime_error:{$this->isIgnoreWorkTimeError()},msg:{$e->getMessage()}");
            return false;
        }
    }

    /**
     * @throws Exception
     */
    private function validOrderState(): void
    {

        if (!in_array($this->order_info->getOrderState(), [
            TripOrderState::ORDER_STATE_TICKETED,
            TripOrderState::ORDER_STATE_CHANGED,
        ])) {
            $this->throwOtherErrorMsg("当前订单状态不可退票");
        }

        $order_train = $this->order_info->getTrainInfo();
        $this->validateOrderTrain($order_train);
    }

    /**
     * 验证发车
     *
     * @param $order_train_info
     *
     * @return void
     * @throws Exception
     */
    private function validateOrderTrain($order_train_info): void
    {
        if ($order_train_info->isDeparted()) {
            $this->throwOtherErrorMsg("列车已发车");
        }

        if (Carbon::parse($order_train_info->getTrainDate())->lessThan(Carbon::now()->addHours(3))) {
            $this->throwOtherErrorMsg("退票只支持距离发车前3个小时以上退票");
        }
    }

    /**
     * @throws Exception
     */
    private function validOrder(): void
    {
        if ($this->order_info->isChangeOrigin() && $this->passenger->getOrderState() == TripOrderState::ORDER_STATE_CHANGED) {
            $this->throwOtherErrorMsg("原单已改签成功,不可退票");
        }
    }

    public function has_ticket(): bool
    {
        return $this->passenger->getTicketSeat() != '';
    }

    private function validatePassengerOrderState(): void
    {
//		if($this->getOrderState()  ){}
    }
}